#!/usr/bin/python
#-*- coding:utf-8 -*-
import functions, wx, sys
class App(wx.App):
	def __init__(self, version, obj):
		wx.App.__init__(self)
		f = obj.Frame(version)
		self.SetTopWindow(f)
#this class show the Bug Dialog that send an e-mail to report a bug
class BugDialog(wx.Dialog):
	def __init__(self, parent=None, title="Report a bug", id=wx.NewId()):
		wx.Dialog.__init__(self, parent, id, title)

		panel = wx.Panel(self, -1)
		
		self.obj = parent

		hbox = wx.BoxSizer(wx.HORIZONTAL)

		fgs = wx.FlexGridSizer(6, 2, 9, 25)

		host = wx.StaticText(panel, -1, 'Host')
		port = wx.StaticText(panel, -1, 'Port')
		app = wx.StaticText(panel, -1, "Use Apport?")
		subject = wx.StaticText(panel, -1, 'Subject')
		program = wx.StaticText(panel, -1, 'Program')
		version = wx.StaticText(panel, -1, 'Version')
		text = wx.StaticText(panel, -1, "Text")

		self.tc1 = wx.TextCtrl(panel, wx.NewId())
		#self.tc1.SetValue("Insert the host (if default out.aliceposta.it not response/send)")
		self.tc2 = wx.TextCtrl(panel, wx.NewId())
		#self.tc2.SetValue("Insert the port (if default 25 not response/send)")
		self.tc3 = wx.TextCtrl(panel, wx.NewId())
		#self.tc3.SetValue("Insert yes or no to use apport to send other anonymous information")
		self.tc4 = wx.TextCtrl(panel, wx.NewId())
		self.tc6 = wx.TextCtrl(panel, wx.NewId())
		self.tc7 = wx.TextCtrl(panel, wx.NewId())
		#self.tc4.SetValue("Insert the subject")
		self.tc5 = wx.TextCtrl(panel, wx.NewId(), style=wx.TE_MULTILINE)
		#self.tc5.SetValue("Insert the text")
		
		b = wx.Button(panel, wx.NewId(), "OK")
		b2 = wx.Button(panel, wx.NewId(), "Cancel")

		fgs.AddMany([(host), (self.tc1, 1, wx.EXPAND), (port), (self.tc2, 1, wx.EXPAND),
		    (app), (self.tc3, 1, wx.EXPAND), (subject), (self.tc4, 1, wx.EXPAND),
		    (program), (self.tc6, 1, wx.EXPAND), (version), (self.tc7, 1, wx.EXPAND),
		    (text), (self.tc5, 1, wx.EXPAND), 
		    (b2), (b, 1, wx.ALIGN_RIGHT)])

		fgs.AddGrowableRow(4, 1)
		fgs.AddGrowableCol(1, 1)

		hbox.Add(fgs, 1, wx.ALL | wx.EXPAND, 15)
		panel.SetSizer(hbox)
		
		self.Bind(wx.EVT_BUTTON, self.OnOk, id=b.GetId())
		self.Bind(wx.EVT_BUTTON, self.OnCancel, id=b2.GetId())
		#self.Bind(wx.EVT_SET_FOCUS, self.OnText, id=self.tc1.GetId())
		#self.Bind(wx.EVT_SET_FOCUS, self.OnText, id=self.tc2.GetId())
		#self.Bind(wx.EVT_SET_FOCUS, self.OnText, id=self.tc3.GetId())
		#self.Bind(wx.EVT_SET_FOCUS, self.OnText, id=self.tc4.GetId())
		#self.Bind(wx.EVT_SET_FOCUS, self.OnText, id=self.tc5.GetId())
		
		self.ShowModal()
		
	def OnCancel(self, event):
		self.Destroy()
	def OnOk(self, event):
		program = self.tc6.GetValue()
		version = self.tc7.GetValue()
		if program == "" or program not in ("wxEdit", "wxTorrent"):
			program = self.obj.GetTitle()
			if len(program) > 6 and program != "wxTorrent":
				program = program[:6]
		if version == "":
			version = self.obj.version
		if functions.sendmail(self.tc1.GetValue(), self.tc5.GetValue(), self.tc4.GetValue(), self.tc2.GetValue(), self.tc3.GetValue(), program, version):
			dlg = wx.MessageDialog(self, "E-mail sended succesfully", "Info", wx.ICON_INFORMATION | wx.OK)
			dlg.ShowModal()
			dlg.Destroy()
			self.Destroy()
		else:
			dlg = wx.MessageDialog(self, "Error: e-mail not sended/not sended correctly.\nControl that the port (default 25) is open and\nthe computer is connected to Internet", "Error", wx.ICON_ERROR | wx.OK)
			dlg.ShowModal()
			dlg.Destroy()
			self.Destroy()
	def OnText(self, event):
		if event.GetId() == self.tc1.GetId():
			self.tc1.ClearAll()
		elif event.GetId() == self.tc2.GetId():
			self.tc2.ClearAll()
		elif event.GetId() == self.tc3.GetId():
			self.tc3.ClearAll()
		elif event.GetId() == self.tc4.GetId():
			self.tc4.ClearAll()
		elif event.GetId() == self.tc5.GetId():
			self.tc5.ClearAll()
class TaskBarIcon(wx.TaskBarIcon):
	def __init__(self, frame, icon="wxsoft16.gif"):
		self.id1 = wx.NewId()
		self.id2 = wx.NewId()
		self.id3 = wx.NewId()
		self.id4 = wx.NewId()
		wx.TaskBarIcon.__init__(self)
		self.frame = frame
		name = self.frame.GetTitle()
		if len(name) > 6 and name not in "wxTorrent":
			name = name[:6]
		self.SetIcon(wx.Icon(self.frame.prefix3+icon, wx.BITMAP_TYPE_GIF), name)
		self.Bind(wx.EVT_MENU, self.OnTaskBarActivate, id=self.id1)
		self.Bind(wx.EVT_MENU, self.OnTaskBarDeactivate, id=self.id2)
		self.Bind(wx.EVT_MENU, self.OnTaskBarClose, id=self.id3)
		self.Bind(wx.EVT_MENU, self.OnTaskBarForceClose, id=self.id4)
	def CreatePopupMenu(self):
		menu = wx.Menu()
		show = wx.MenuItem(menu, self.id1, "&Show", "Show the window")
		menu.AppendItem(show)
		hide = wx.MenuItem(menu, self.id2, "&Hide", "Hide the window")
		menu.AppendItem(hide)
		menu.AppendSeparator()
		quit = wx.MenuItem(menu, self.id3, "&Quit", "Close the program")
		quit.SetBitmap(wx.Image(self.frame.prefix1+'application-exit.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap())
		menu.AppendItem(quit)
		force_quit = wx.MenuItem(menu, self.id4, "&Forced Quit", "Force the closure of the program")
		menu.AppendItem(force_quit)
		return menu
	def OnTaskBarActivate(self, event):
		if not self.frame.IsShown():
			self.frame.Show()
	def OnTaskBarClose(self, event):
		self.frame.OnClose()
	def OnTaskBarDeactivate(self, event):
		if self.frame.IsShown():
			self.frame.Hide()
	def OnTaskBarForceClose(self, event):
		sys.exit(0)


